//
//  SXTLoginViewController.m
//  SXT
//
//  Created by 赵金鹏 on 16/8/21.
//  Copyright © 2016年 赵金鹏. All rights reserved.
//

#import "SXTLoginViewController.h"
#import "SXTLoginView.h"
#import "SXTLandingViewController.h"
//
#import "LZSPwdViewController.h"
//#import "SXTThirdLoginView.h"
//#import "UMSocial.h"//引入友盟的qq登陆

@interface SXTLoginViewController ()
//@property (strong, nonatomic)   SXTThirdLoginView *thirdLoginView;              /** 第三方登录view */
@property (strong, nonatomic)   SXTLoginView * loginView;   /** 登录view */
@end

@implementation SXTLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登录";
    self.edgesForExtendedLayout = 0;
    [self addController];
    // Do any additional setup after loading the view.
    
    if (self.isRegistPushToLogin) {
        
        _loginView.goLandingBtn.hidden = YES;
    }
    
    //
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(RegistSuccess:) name:@"RegistSuccess" object:nil];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"RegistSuccess" object:nil];
}

-(void)RegistSuccess:(NSNotification *)notic{
    
    dispatch_async(dispatch_get_main_queue(), ^{
    
        _loginView.userNameText.text = notic.userInfo[@"userName"];
        _loginView.passwordText.text = notic.userInfo[@"password"];
        [_loginView passwordTextChangeText:_loginView.passwordText];
    });
}

- (void)addController{
    [self.view addSubview:self.loginView];
//    [self.view addSubview:self.thirdLoginView];
    
    __weak typeof (self) weakSelf = self;
    [_loginView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(weakSelf.view);
        make.height.equalTo(@200);
    }];
}

- (SXTLoginView *)loginView{
    if (!_loginView) {
        _loginView = [[SXTLoginView alloc]init];
        __weak typeof (self) weakSelf = self;
        _loginView.loginBlock = ^(NSDictionary *dic){
            [weakSelf loginMethod:dic];
        };
        [_loginView.goLandingBtn addTarget:self action:@selector(goLandingBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        //
        [_loginView.pwdBtn addTarget:self action:@selector(goFixPwdBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginView;
}

-(void)goLandingBtnClick:(UIButton *)btn{
    SXTLandingViewController *landingVC = [[SXTLandingViewController alloc] init];
    landingVC.isLoginPushToRegist = YES;
    [self.navigationController pushViewController:landingVC animated:YES];
}

-(void)goFixPwdBtnClick:(UIButton *)btn{
    LZSPwdViewController *pwdVC = [[LZSPwdViewController alloc] init];
//    landingVC.isLoginPushToRegist = YES;
    [self.navigationController pushViewController:pwdVC animated:YES];
}

- (void)loginMethod:(NSDictionary *)dic{
    //    http://123.57.141.249:8080/beautalk/appMember/appLogin.do

    NSString *userName = dic[@"LoginName"];
    NSString *password = dic[@"Lpassword"];
//    //从注册那里获得数据
//    //先保存用户名
//  NSString *usernamestr = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
//    NSString *userNamestr;
//    NSString *passwordstr;
//    //
//    if(userName.length>0){
//        //根据用户名保存用户数据
//        NSDictionary *dics = [[NSUserDefaults standardUserDefaults] objectForKey:usernamestr];
//        userNamestr = dics[@"userName"];
//        passwordstr = dics[@"password"];
//    }

    
    if (userName.length<11) {
        [self.view makeToast:@"手机号码错误" duration:0.5 position:CSToastPositionCenter];
        return ;
    }
    
//    if (![userName isEqualToString:userNamestr]) {
//        [self.view makeToast:@"手机号码未注册" duration:0.5 position:CSToastPositionCenter];
//        return;
//    }
    
    if (password.length<6) {
        [self.view makeToast:@"密码错误" duration:0.5 position:CSToastPositionCenter];
        return ;
    }
    
//    if (![password isEqualToString:passwordstr]) {
//        [self.view makeToast:@"密码错误" duration:0.5 position:CSToastPositionCenter];
//        return;
//    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"key"] = @"cp";
    params[@"action"] = @"login";
    
    params[@"tel"] = userName;
    params[@"password"] = password;
    
    [self postData:@"http://guleiba.com/api/caipiao/user/" param:params success:^(id responseObject) {
        NSLog(@"%@",responseObject);
        if ([responseObject[@"code"] integerValue] == 1) {
            [self showTostMessage:@"登录成功"];
            NSDictionary *data = responseObject[@"data"];
            //
            NSDictionary *newdic = @{@"LoginName":userName,@"Lpassword":password,@"name":data[@"name"]};
            [[NSUserDefaults standardUserDefaults] setObject:newdic forKey:@"ISLOGIN"];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"LOGINSUCCESS" object:nil];
            [self performSelector:@selector(popMyViewController) withObject:nil afterDelay:1.0];
        }else{
            [self showTostMessage:responseObject[@"msg"]];
//            [[NSUserDefaults standardUserDefaults] setObject:dic forKey:@"ISLOGIN"];
//            [[NSNotificationCenter defaultCenter] postNotificationName:@"LOGINSUCCESS" object:nil];
//            [self performSelector:@selector(popMyViewController) withObject:nil afterDelay:1.0];
//            [self.view makeToast:@"失败" duration:0.5 position:CSToastPositionCenter];
        }

    } error:^(NSError *error) {
        [self showTostMessage:error.domain];
//        [self.view makeToast:error.domain duration:0.5 position:CSToastPositionCenter];
//        [self showTostMessage:@"登录成功"];
//        [[NSUserDefaults standardUserDefaults] setObject:dic forKey:@"ISLOGIN"];
//        [[NSNotificationCenter defaultCenter] postNotificationName:@"LOGINSUCCESS" object:nil];
//        [self performSelector:@selector(popMyViewController) withObject:nil afterDelay:1.0];
    }];
}

- (void)popMyViewController{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
#pragma mark - qq登录
- (void)qqLandingMethod{
//    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToQQ];
//    __weak typeof (self) weakSelf = self;
//    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){        
//        if (response.responseCode == UMSResponseCodeSuccess) {
//            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:snsPlatform.platformName];
////            SXTLandingNextViewController *next = [[SXTLandingNextViewController alloc]init];
////            next.userMessageDic = @{@"userName":snsAccount.userName,@"iconImage":snsAccount.iconURL,@"Telephone":@"17721025595",@"password":@"123123"};
////            [weakSelf.navigationController pushViewController:next animated:YES];
//        }});
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
